<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Events extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
			$this->load->library('session');
		}
		
		function index($query_id=0, $order_by='created_date', $sort_order='asc', $current_page=0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					}
				}
				
			$results_per_page = 2;
		
		
			$url = CURL_URL;
			$data = array( 'www-command'=>'events-all_events','results_per_page'=>$results_per_page,'current_page'=>$current_page,'order_by'=>$order_by,'sort_order'=>$sort_order,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			if($response->message !== 'No Events'){
				$data['event_all_events'] = $response;
				
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
			
				$this->load->library('pagination');			
				$config['base_url'] = base_url().'admin/events/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
				$config['per_page'] = $results_per_page;
				$config['total_rows']  = $response->total;
				$config['uri_segment'] = 7;
				$config['num_links'] = 1;
				$config['prev_link'] = 'Previous';
				$config['next_link'] = 'Next';
				$config['first_link'] = 'First';
				$config['last_link'] = 'Last';
				$this->pagination->initialize($config);
				$data['pagination'] = $this->pagination->create_links();
				$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['event_all_events'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['event_fields'] = array('event_title'=>'Title', 'event_location'=>'Location','status'=>'Status');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$data['status'] = array(1=>'Active', 0=>'In-active');
			$this->load->view('admin/events', $data);
 		}
			
		function search_by(){
			if($this->input->post('search') == ''){
				$search = $this->input->post('status');
			}else{
				$search = $this->input->post('search');	
			}
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/events/index/'.$query_id);
			
		}
		
			function create_events(){
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('title','Title','required');
				$this->form_validation->set_rules('event_type','','callback_event_type');
				$this->form_validation->set_rules('assc','','callback_assc');
				$this->form_validation->set_rules('credit','Credit','required|numeric|max_length[2]');
				$this->form_validation->set_rules('description','Description','required|max_length[500]');
				$this->form_validation->set_rules('url','','callback_valid_url_format');
				if($this->form_validation->run() == false){
					//echo 'exit';exit;
					$url = CURL_URL;
					$data = array('www-command'=>'events-get_event_type_list');
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$event_type_list = json_decode(curl_exec($ch));
					//echo '<pre>';print_r($event_type_list);exit;
					$data = array('www-command'=>'events-get_association_list');
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$get_association_list = json_decode(curl_exec($ch));
					//echo '<pre>';print_r($get_association_list);exit;
					
					//echo '<pre>';print_r($get_location);exit;
					$data['event_type_list'] = $event_type_list;
					$data['get_association_list'] = $get_association_list;
					/* $value = '1-movies';
					$value = explode("-", $value);
					echo $value[0];exit; */
					//echo '<pre>';print_r($get_association_list);exit;
					$this->load->view('admin/create_events_page', $data);
				}else{
					//echo 'hi';exit;
					$event_type = explode('-',$this->input->post('event_type'));
					//echo '<pre>';print_r($_FILES);
					//echo '<pre>';print_r($_POST);
					
					if($this->input->post('upcoming') == 'on'){
						$upcoming = 1;
					}else{
						$upcoming = 0;
					}
					
					if($this->input->post('donate') == 'on'){
						$donate = 1;
					}else{
						$donate = 0;
					}
					
					foreach($_POST['stop_ticket'] as $stop_ticket){
							if($stop_ticket == 'on'){
								$ticket[] = 1;
							}else{
								$ticket[] = 0;
							}
						}
						//echo '<pre>';print_r($ticket);exit;
					$data = array('www-command'=>'events-create_event',
									'event_title'=>$this->input->post('title'),
									'event_type_id'=>$event_type[0],
									'association_id'=>$this->input->post('assc'),
									'credit_card_charges'=>$this->input->post('credit'),
									'event_description'=>$this->input->post('description'),
									'sponsor_url'=>$this->input->post('url'),
									'event_image'=>$_FILES['event_image'],
									'sponsor_logo'=>$_FILES['sponsor'],
									'sponsor_email_logo'=>$_FILES['sponsor_email'],
									'flyer_logo'=>$_FILES['flyer'],
									'is_upcomming_event'=>$upcoming,
									'donate'=>$donate,
									'event_pricing_id'=>implode(',',$_POST['price']),
									'number_of_tickets'=>implode(',',$_POST['ticket']),
									'event_time'=> implode(',',$_POST['time']),
									'event_date'=>implode(',',$_POST['date']),
									'location_id'=>implode(',',$_POST['location']),
									'stop_ticket'=>implode(',',$ticket)
									);
						
						
					if($event_type[1] == 'movies'){
						
						$star_image = $_FILES['star_image'];
						$star_image['title'] = $_POST['star_cast'];
						//echo '<pre>';print_r($star_image);
						
						$data['director'] = $this->input->post('director');
						$data['producer'] = $this->input->post('producer');
						$data['music_director'] = $this->input->post('music_director');
						$data['release_date'] = $this->input->post('release_date');
						$data['star_cast'] = $this->input->post('starcast');
						$data['movie_image'] = $_FILES['movie_image'];
						$data['starcast_image'] = $star_image;
						$data['video_name'] = implode(',',$_POST['video_name']);
						$data['video_url'] = implode(',',$_POST['video_url']);
						$data['video_image_url'] = implode(',',$_POST['video_image']);						
					}
				//	echo '<pre>';print_r($data);exit;
					$url = CURL_URL;
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					redirect('admin/events/index/');
				}
			}
			function event_type($type){
				if($type == ''){
					$this->form_validation->set_message('event_type','Please select event type');
					return false;
				}else{
					return true;
				}
			}
			
			function assc($assc){
				if($assc == ''){
					$this->form_validation->set_message('assc','Please select association');
					return false;
				}else{
					return true;
				}
			}
			
			function valid_url_format($url){
				if($url == ''){
					return true;
				}
			$pattern = '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
			if(!preg_match($pattern, $url)){
				$this->form_validation->set_message('valid_url_format', 'The URL you entered is not accessible.');
				return false;
				}
				return true;
		}
			
			function add_location(){
				$url = CURL_URL;
				$data = array('www-command'=>'events-get_event_location_list');
				 //echo json_encode($data);exit;
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$get_event_location_list = curl_exec($ch);
				//$get_event_location_list = (array)$get_event_location_list;
				
				$data = array('www-command'=>'events-get_event_price_list');
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$get_event_price_list = curl_exec($ch);
				echo $get_event_location_list.'split'.$get_event_price_list;
			}
			
			function event_pricing(){
				$url = CURL_URL;
				$data = array('www-command'=>'events-get_event_pricing','event_pricing_id'=>$this->uri->segment(4));
				 //echo json_encode($data);exit;
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$get_event_pricing = curl_exec($ch);
				echo $get_event_pricing;
				
			}
			
			function edit_events(){
				//echo 'hi';exit;
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('title','Title','required');
				$this->form_validation->set_rules('event_type','','callback_event_type');
				$this->form_validation->set_rules('assc','','callback_assc');
				$this->form_validation->set_rules('credit','Credit','required|numeric|max_length[2]');
				$this->form_validation->set_rules('description','Description','required|max_length[500]');
				$this->form_validation->set_rules('url','','callback_valid_url_format');
				if($this->form_validation->run() == false){
					$url = CURL_URL;
					if($this->uri->segment(4) > 0){
						$_SESSION['events-id'] = $this->uri->segment(4);
					}
					$data = array('www-command'=>'events-get_event','event_id'=>$_SESSION['events-id']);
					 //echo json_encode($data);exit;
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$events_get_event = json_decode(curl_exec($ch));
					//echo '<pre>';print_r($events_get_event);exit;
				
					$data = array('www-command'=>'events-get_event_type_list');
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$event_type_list = json_decode(curl_exec($ch));
					//echo '<pre>';print_r($event_type_list);exit;
					$data = array('www-command'=>'events-get_association_list');
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$get_association_list = json_decode(curl_exec($ch));
					//echo '<pre>';print_r($data);exit;
					$url = CURL_URL;
					$data = array('www-command'=>'events-get_event_location_list');
					 //echo json_encode($data);exit;
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$get_event_location_list = json_decode(curl_exec($ch));
					//$get_event_location_list = (array)$get_event_location_list;
					
					$data = array('www-command'=>'events-get_event_price_list');
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$get_event_price_list = json_decode(curl_exec($ch));
					
					
					$data['event_type_list'] = $event_type_list;
					$data['get_association_list'] = $get_association_list;
					$data['events_get_event'] = $events_get_event;
					$data['get_event_price_list'] = $get_event_price_list;
					$data['get_event_location_list'] = $get_event_location_list;
					
					$this->load->view('admin/edit_events_page',$data);
				}else{
					//echo '<pre>';print_r($_POST);
					$event_type = explode('-',$this->input->post('event_type'));
					
					if($this->input->post('upcoming') == 'on'){
						$upcoming = 1;
					}else{
						$upcoming = 0;
					}
					
					if($this->input->post('donate') == 'on'){
						$donate = 1;
					}else{
						$donate = 0;
					}
					
					foreach($_POST['stop_ticket'] as $stop_ticket){
							if($stop_ticket == 'on'){
								$ticket[] = 1;
							}else{
								$ticket[] = 0;
							}
						}
					
					//echo '<pre>';print_r($ticket);exit;
									
					foreach($this->input->post('location') as $id=>$loc_id){
						$event_location[] = array(
						'location_id'=>$loc_id,
						'event_pricing_id'=>$this->input->post('price')[$id],
						'number_of_tickets'=>$this->input->post('ticket')[$id],
						'event_time'=>$this->input->post('time')[$id],
						'event_date'=>$this->input->post('date')[$id],
						'stop_ticket'=>$ticket[$id]
						);						
						if(isset($_POST['hide_time_id'][$id])){
							$event_location[$id]['event_location_time_id'] = $this->input->post('hide_time_id')[$id]; 
						}
						
					}
					
					
					$data = array('www-command'=>'events-edit_event',
									'event_id'=>$_SESSION['events-id'],
									'event_title'=>$this->input->post('title'),
									'event_type_id'=>$event_type[0],
									'association_id'=>$this->input->post('assc'),
									'description'=>$this->input->post('description'),
									'credit_card_charges'=>$this->input->post('credit'),
									'is_upcomming_event'=>$upcoming,
									'donate'=>$donate,
									'sponsor_url'=>$this->input->post('url'),
									'event_image'=>$_FILES['event_image'],
									'sponsor_logo'=>$_FILES['sponsor'],
									'sponsor_email_logo'=>$_FILES['sponsor_email'],
									'flyer_logo'=>$_FILES['flyer'],
									'event_locations'=>$event_location,
									'remove_location'=>implode(',',$this->input->post('remove_loc_id'))
									);
								//echo '<pre>';print_r(json_encode($data['event_locations']));exit;
									
					if($event_type[1] == 'movies'){
						$data['director'] = $this->input->post('director');
						$data['producer'] = $this->input->post('producer');
						$data['music_director'] = $this->input->post('music_director');
						$data['release_date'] = $this->input->post('release_date');
						$data['star_cast'] = $this->input->post('starcast');
						$data['movie_detail_id'] = $this->input->post('movie_detail_id');
						$new_movie_image = array_slice($_FILES['movie_image']['name'], 0, count($this->input->post('hide_movie')));
						$new_movie_image = array_filter($new_movie_image);
						$movie_id = $_POST['hide_movie'];
						foreach($_FILES['movie_image']['name'] as $k=>$movie){
							if($movie == ''){
								unset($movie_id[$k]);
							}
						}
						$movie_fileupload = array('name'=>array_filter($_FILES['movie_image']['name']),'tmp_name'=>array_filter($_FILES['movie_image']['tmp_name']));
						$data['movie_image_id'] = implode(',',$movie_id);
						$data['movie_name'] = implode(',',$new_movie_image);
						$data['movie_image'] = $movie_fileupload;
						$data['movie_remove_id'] = implode(',',$this->input->post('remove_movie_id'));
						$new_star_image = array_filter(array_slice($_FILES['star_image']['name'], 0, count($this->input->post('hide_star_cast'))));
						$star_cast_id = $_POST['hide_star_cast'];
						$star_cast = $_POST['star_cast'];
						foreach($_FILES['star_image']['name'] as $k=>$star_image){
							if($star_image == ''){
								unset($star_cast_id[$k]);
								unset($star_cast[$k]);
							}
						}
						$movie_fileupload = array('name'=>array_filter($_FILES['star_image']['name']),'tmp_name'=>array_filter($_FILES['star_image']['tmp_name']));
						$movie_fileupload['title'] = $star_cast;
						
						$data['movie_starcast_image_id'] = implode(',',$star_cast_id);
						$data['starcast_image_name'] = implode(',',$new_star_image);
						$data['starcast_image'] = $movie_fileupload;
						$data['starcast_remove_id'] = implode(',',$this->input->post('remove_star_id'));
						
						if(isset($_POST['video_name'])){
							foreach($_POST['video_name'] as $id=>$val){
								$video[] = array('video_name'=>$this->input->post('video_name')[$id],
													'video_url'=>$this->input->post('video_url')[$id],
													'video_image_url'=>$this->input->post('video_image')[$id],
													); 
							if(isset($_POST['hide_video'][$id])){
								$video[$id]['movie_youtube_video_id'] = $this->input->post('hide_video')[$id];
							}
							}
						$data['youtube_videos'] = $video;
						$data['youtube_remove_id'] = implode(',',$this->input->post('remove_video_id'));
							//echo '<pre>';print_r($video);exit;
						}
					}
					$url = CURL_URL;
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response = json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/events/index/');			
				}
			}
			
			function delete_events(){
				if($this->uri->segment(5) == 0){
						$status = 1;
					}else{
						$status = 0;
					}
				$url = CURL_URL;
				$data = array('www-command'=>'events-delete_event','event_id'=>$this->uri->segment(4),'status'=>$status);
				
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				redirect('admin/events/index');
			}
}